﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using System.Data;
using System.Xml;

namespace SharePointListSync.Actions.Controls
{
    public partial class ACListSync : UserControl
    {
        public SharePointListDetails.SPConnectionsRow ListRow;
       
        DataManager dManager = new DataManager();
        VOlistDetails mVOlistDetails = null;
        const string errorColumnInUpdateLog = "Error";
        private bool iserror; 


        public ACListSync()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Globals.ThisWorkbook.hlp_setView(ACControls.ACListSelectors);
            Globals.ThisWorkbook.mACBody.mACListSelector.hlp_ToggelGroupBox(false);
            Globals.ThisWorkbook.mACBody.mACListSelector.NavigatedBack();
        }

        #region Queryuing and Populate Data 


        internal void GetDataListView(ref VOlistDetails _mVOlistDetails)
        {
            if(mVOlistDetails != null) mVOlistDetails.ResetBeforUse(); 
            mVOlistDetails = _mVOlistDetails; 
            //reset all valus 
            Globals.ThisWorkbook.DSlists.SPResult.Rows.Clear();
            Globals.ThisWorkbook.DSlists.SPResult.Columns.Clear();
            Globals.ThisWorkbook.DSlists.SPResult.AcceptChanges();

            // set the Value 
            ConfiguredDataSet();
            WSGetDataListView();
            BindDataToSheet();
        }

        internal void GetDataAllColumns(ref VOlistDetails _mVOlistDetails)
        {
            if (mVOlistDetails != null) mVOlistDetails= null;
            mVOlistDetails = _mVOlistDetails;

            Globals.ThisWorkbook.DSlists.SPResult.Rows.Clear();
            Globals.ThisWorkbook.DSlists.SPResult.Columns.Clear();

            // set the Value 
            ConfiguredDataSet();
            WSGetDataallCol();
            BindDataToSheet();
        }

        #region HelperCalss Queryuing and Populate Data



        private void ConfiguredDataSet()
        {
            foreach (KeyValuePair<string, string> colname in mVOlistDetails.ColumnToShow)
            {
                Globals.ThisWorkbook.DSlists.SPResult.Columns.Add(colname.Key);
            }
            Globals.ThisWorkbook.DSlists.SPResult.AcceptChanges();
        }

        private void BindDataToSheet()
        {

            Globals.Sheet1.list1.DataSource = Globals.ThisWorkbook.DSlists.SPResult;
            Globals.Sheet1.list1.AutoSelectRows = true;
            Globals.Sheet1.list1.AutoSetDataBoundColumnHeaders = true;
        }
        
        private void PopulateDataset(XmlNode ndItems, Dictionary<string, string> selectedColumns)
        {

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(ndItems.OuterXml);
            XmlNamespaceManager namespaceMgr = new XmlNamespaceManager(xmlDoc.NameTable);
            namespaceMgr.AddNamespace(Globals.ThisWorkbook.ListItemsNamespacePrefix, Globals.ThisWorkbook.ListItemsNamespaceUri);
            XmlNodeList itemsNodeList = xmlDoc.SelectNodes("//z:row", namespaceMgr);


            foreach (XmlNode itemNode in itemsNodeList)
            {
                SharePointListDetails.SPResultRow row = Globals.ThisWorkbook.DSlists.SPResult.NewSPResultRow();
                foreach (KeyValuePair<string, string> fieled in selectedColumns)
                {
                    string owsfieled = "ows_" + fieled.Value;
                    if (itemNode.Attributes[owsfieled] != null)
                    {
                        DataColumn dc = Globals.ThisWorkbook.DSlists.SPResult.Columns[fieled.Key];
                        row[dc] = itemNode.Attributes[owsfieled].InnerText;
                    }
                }
                Globals.ThisWorkbook.DSlists.SPResult.Rows.Add(row);
            }
            Globals.ThisWorkbook.DSlists.SPResult.AcceptChanges();
        }

        private void WSGetDataListView()
        {
            XmlNode ndItems = dManager.GetListItems(mVOlistDetails.SelecteConnectionRow, mVOlistDetails.ViewID, mVOlistDetails.ViewName, mVOlistDetails.ContentTypeName, mVOlistDetails.ContentTypeID,mVOlistDetails.RowLimit);
            PopulateDataset(ndItems, mVOlistDetails.ColumnToShow);
        }

        private void WSGetDataallCol()
        {
            XmlNode ndItems = dManager.GetListItemsAllColumn(mVOlistDetails.SelecteConnectionRow, mVOlistDetails.ColumnToShow, mVOlistDetails.ViewID, mVOlistDetails.ViewName, mVOlistDetails.ContentTypeName, mVOlistDetails.ContentTypeID, mVOlistDetails.RowLimit);
            PopulateDataset(ndItems, mVOlistDetails.ColumnToShow);
        }

        #endregion

       

        #endregion

        #region Upload Oppration

        private void BtnSyncToList_Click(object sender, EventArgs e)
        {
            DataTable allChanges = Globals.ThisWorkbook.DSlists.SPResult.GetChanges();
            if (allChanges != null)
            {

                XmlDocument batch = dManager.CreateBatchDefination();
                DataView dv = new DataView(allChanges, String.Empty, String.Empty, DataViewRowState.CurrentRows);

                if (dv.Count > 0)
                {
                    Hlp_createBatchCmd(dv, ref batch, ref mVOlistDetails);
                }
                dManager.CommitBatch(batch, ref mVOlistDetails);
            }

        }

        private void Hlp_createBatchCmd(DataView dv, ref XmlDocument batch, ref VOlistDetails mVOlistDetails)
         {
             foreach (DataRowView drv in dv)
             {
                 SharePointListDetails.SPResultRow row = (SharePointListDetails.SPResultRow)drv.Row;
                 switch (row.RowState)
                 {
                     case DataRowState.Added:
                         dManager.Batchcmd(drv, ref batch, ref mVOlistDetails, DataRowState.Added);
                         break;
                     case DataRowState.Deleted:
                         dManager.Batchcmd(drv, ref batch, ref mVOlistDetails, DataRowState.Deleted);
                         break;

                     case DataRowState.Modified:
                         dManager.Batchcmd(drv, ref batch, ref mVOlistDetails, DataRowState.Modified);
                         break;
                     default:
                         break;
                 }
             }
         }


        #endregion
    }
}
